﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace KZ.Express.Setup.Partner
{
    public partial class CustomerSelectView : FormBase
    {
        #region Private Variables

        private string _partnerID;
        private string _partnerCode;
        private List<DataRow> _selectedRow;

        #endregion Private Variables

        #region Private Methods

        private void CustomerSelectView_Load(object sender, EventArgs e)
        {
            _selectedRow = new List<DataRow>();
            LoadData();
        }

        private void dtgCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colChk.Index && e.RowIndex != -1)
            {
                bool value = Utility.BoolParse(dtgCustomer[colChk.Index, e.RowIndex].Value);
                if (value)
                {
                    dtgCustomer[colChk.Index, e.RowIndex].Value = false;
                    _selectedRow.Remove(base._table.Rows[e.RowIndex]);
                }
                else
                {
                    dtgCustomer[colChk.Index, e.RowIndex].Value = true;
                    _selectedRow.Add(base._table.Rows[e.RowIndex]);
                }
                dtgCustomer.EndEdit();
            }
        }

        private void txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            base.LoadData();
        }

        private void txtCustomerCode_TextChanged(object sender, EventArgs e)
        {
            base.LoadData();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            _partnerID = _partnerCode = String.Empty;
            foreach (DataRow row in _selectedRow)
            {
                _partnerID += "," + row["PartnerID"];
                _partnerCode += "; " + row["PartnerCode"];
            }
            if (_selectedRow.Count > 0)
            {
                _partnerID = _partnerID.Remove(0, 1);
                _partnerCode = _partnerCode.Remove(0, 2);
            }

            DialogResult = DialogResult.Yes;
        }

        protected override void LoadData()
        {
            _selectedRow = new List<DataRow>();
            base.LoadData();

            string temp = "," + _partnerID + ",";
            foreach (DataGridViewRow row in dtgCustomer.Rows)
            {
                string partnerID = "," + Utility.StringParse(row.Cells[colPartnerID.Index].Value) + ",";
                if (temp.Contains(partnerID))
                {
                    row.Cells[colChk.Index].Value = true;
                    _selectedRow.Add(base._table.Rows[row.Index]);
                }
            }
        }

        #endregion Private Methods

        #region Constructors

        public CustomerSelectView()
        {
            InitializeComponent();
            base.TopLevel = true;

            base.OrderBy = "PartnerCode";
            base.OrderDirection = "ASC";

            base.InitializeDataGrid(dtgCustomer);
            base._presenter = new PresenterBase(this, new CustomerENT());

            btnViewAll.Click += btnViewAll_Click;
            dtgCustomer.ColumnHeaderMouseClick += dtg_ColumnHeaderMouseClick;

            Global.SetDoubleBuffered(dtgCustomer);
        }

        #endregion Constructors

        #region Public Properties

        public string SelectedPartnerID
        {
            get { return _partnerID; }
            set { _partnerID = value; }
        }

        public string SelectedPartnerCode
        {
            get { return _partnerCode; }
        }

        [Search]
        public string PartnerCode
        {
            get { return txtCustomerCode.Text; }
            set { txtCustomerCode.Text = value; }
        }

        [Search]
        public string PartnerName
        {
            get { return txtCustomerName.Text; }
            set { txtCustomerName.Text = value; }
        }

        [Search]
        public bool IsCustomer
        {
            get { return true; }
        }

        #endregion Public Properties
    }
}